﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Caching;

namespace GiaoTrinhEPU.Repositories.Repositories
{
    public class PrintRepository : BaseRepository, IPrintRepository
    {
        public PrintRepository() : base() { }
        public PrintRepository(ICacheProvider cacheProvider) : base(cacheProvider) { }

        /// <summary>
        /// Clear cache of all banks
        /// </summary>
        public void ClearCache()
        {
            Cache.Invalidate("prints");
        }

        /// <summary>
        /// Clear cache of the print
        /// </summary>
        /// <param name="key"></param>
        public void ClearCache(String key)
        {
            Cache.Invalidate("print-" + key);
        }
        /// <summary>
        /// Create a new bank
        /// </summary>
        /// <param name="item"></param>
        public void CreatePrint(Print item)
        {
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    db.Prints.InsertOnSubmit(item);
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
        }

        // <summary>
        /// Update Print
        /// </summary>
        /// <param name="item"></param>
        public void UpdatePrint(Print item)
        {
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var print = db.Prints.Single(a => a.Id.Equals(item.Id));
                    print.Id = (int)item.Id;
                    print.Name = item.Name;
                    print.Description = item.Description;
                    print.PDate = item.PDate;
                    print.Quantum = item.Quantum;
                    print.CreatedBy = item.CreatedBy;
                    print.CreatedDate = item.CreatedDate;
                    print.ModifiedBy = item.ModifiedBy;
                    print.ModifiedCount = item.ModifiedCount;
                    print.ModifiedDate = (DateTime)item.ModifiedDate;
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
        }
        /// <summary>
        /// Delete a Print
        /// </summary>
        /// <param name="id"></param>
        public void DeletePrint(int id)
        {
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var print = db.Prints.Single(a => a.Id.Equals(id));
                    db.Prints.DeleteOnSubmit(print);
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
        }
        /// <summary>
        /// List all Print in database, order by Print's name
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Print> ListPrints(bool cacheEnabled = true)
        {
            if (cacheEnabled == true)
            {
                // if it's not in the cache, we need to read it form the repository// First, check the cache
                IEnumerable<Print> printData = Cache.Get("prints") as IEnumerable<Print>;

                // if it's not in the cache, we need to read it form the repository
                if (printData == null)
                {
                    //Get the repository data
                    try
                    {
                        using (var db = new ThuVienEPUDataContext())
                        {
                            db.DeferredLoadingEnabled = false;
                            printData = db.Prints.OrderBy(a => a.Name).ToList();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Writer.Write(ex.Message, ex.Source);
                    }

                    if (printData.Any())
                    {
                        Cache.Set("prints", printData, GiaoTrinhEPU.Caching.Properties.Settings.Default.CacheTime);
                    }
                }
                return printData;
            }
            else
            {
                //Get the repository data
                IEnumerable<Print> printData = null;

                try
                {
                    using (var db = new ThuVienEPUDataContext())
                    {
                        printData = db.Prints.OrderBy(a => a.Name).ToList();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Writer.Write(ex.Message, ex.Source);
                }

                return printData;
            }
        }
        /// <summary>
        /// Get a Print if we know it's id
        /// </summary>
        /// <param name="id">id of the banner</param>
        /// <returns>
        /// return a banner
        /// </returns>
        public Print GetPrintById(int id)
        {
            Cache.Invalidate("print-" + id);
            Print printData = Cache.Get("print-" + id) as Print;
            if (printData == null)
            {
                try
                {
                    using (var db = new ThuVienEPUDataContext())
                    {
                        printData = db.Prints.Single(a => a.Id.Equals(id));
                    }
                }
                catch (Exception ex)
                {
                    Logger.Writer.Write(ex.Message, ex.Source);
                }

                if (printData != null)
                {
                    Cache.Set("print-" + id, printData, GiaoTrinhEPU.Caching.Properties.Settings.Default.CacheTime);
                }
            }
            return printData;
        }
        /// <summary>
        /// check name of the print already exist in database 
        /// </summary>
        /// <param name="name"></param>
        /// <returns>
        /// return true if exist, else false
        /// </returns>
        public bool HasNameExisted(string name)
        {
            bool isExisted = false;
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var list = (from a in db.Prints
                                where a.Name.Equals(name)
                                select a.Id).ToList();
                    isExisted = list.Count > 0;
                }
            }
            catch (Exception ex)
            {
                Logger.Writer.Write(ex.Message, ex.Source);
            }
            return isExisted;
        }

        /// <summary>
        /// check duplication of the banner in case of updating the banner
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(int id, string name)
        {
            bool isDuplicated = false;
            try
            {
                using (var db = new ThuVienEPUDataContext())
                {
                    var list = (from a in db.Prints
                                where a.Name.Equals(name)
                                where !a.Id.Equals(id)
                                select a.Id).ToList();
                    isDuplicated = list.Count > 0;
                }
            }
            catch (Exception ex)
            {

                Logger.Writer.Write(ex.Message, ex.Source);
            }
            return isDuplicated;
        }

    }
}
